/*
 * Project: a2
 * Copyright (C) 2008 ralfoide gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alfray.a2.gameplay;

import com.alfray.a2.gameplay.AnimThread.AnimType;


/**
 * Rendering state of a sprite.
 * <p/>
 * Important: sprite is initially <em>not</em> visible.
 */
public class SpriteState {

    /** Sprite has no GL effect. */
    public final static int NO_EFFECT = 0;
    /** Sprite rotates on Y using xRotY. */
    public final static int ROT_Y = 0x01;
    /** Sprite rotates on Z using xRotZ. */
    public final static int ROT_Z = 0x02;
    /** Sprite translates in X/Y. */
    public final static int TRANSLATE = 0x04;
    /** Sprite shrinks (zoom) in X/Y */
    public final static int ZOOM = 0x08;
    /** Sprite automatically rotates on Y using mFactorAutoRotY. */
    public final static int AUTO_ROT_Y = 0x10;


    /** Sprite id to display. -1 to disable this sprite. */
    public int mSpriteId = -1;
    /** Sprite animation. Default is {@link GlEffect#NO_EFFECT} */
    public int mGlEffect = NO_EFFECT;
    /** Sprite rotation on Y. Use {@link #setRotationY(int)} to set. */
    public int xRotY;
    /** Sprite rotation on Z. Use {@link #setRotationZ(int)} to set. */
    public int xRotZ;
    /** Current color index. -1 if unknown. Used solely to compare "similar" sprites.
     *  For the actual rendering color use {@link #mColor}. */
    public int mColorIndex = -1;
    /** GL object diffuse color. Default is {@link GameState#WHITE}. */
    public int[] mColor = GameState.WHITE;
    /** Animation type. null if not animation. */
    public AnimType mAnimType;
    /** Opaque data for current animation. */
    public Object mAnimData;
    /** True if sprite is visible. */
    public boolean mIsVisible = false;
    /** GL XY Zoom level. Default is 1<<16 (no zoom), in fp16 GL coordinates. */
    public int xZoomXY;
    /** GL Offset X, in fp16 GL coordinates. */
    public int xOffsetX;
    /** GL Offset Y, in fp16 GL coordinates. */
    public int xOffsetY;
    /** Factor for automatic rotation on Y if > 0. */
    public int mFactorAutoRotY;

    public SpriteState() {
    }

    /** Sprite id to display. -1 to disable this sprite. */
    public void setSpriteId(int spriteId) {
        mSpriteId = spriteId;
    }

    /** Sprite GL animation. This sets one bit in the bitfield. */
    public void setAnimation(int glEffect, boolean set) {
        if (set) mGlEffect |= glEffect;
        else mGlEffect &= ~glEffect;
    }

    /** Sprite GL animation. Clears all bits in the bitfield. */
    public void resetAnimation() {
        mGlEffect = NO_EFFECT;
    }

    /** 3d object diffuse color. Must be one of {@link GameState#BASE_COLORS}.
     * @param colorIndex */
    public void setColor(int colorIndex, int[] color) {
        mColorIndex = colorIndex;
        mColor = color;
    }

    public void setRotationY(int xRotY) {
        this.xRotY = xRotY;
        setAnimation(ROT_Y, xRotY != 0);
    }

    public void setAutoRotationY(int factorY) {
        mFactorAutoRotY = factorY;
        setAnimation(AUTO_ROT_Y, factorY != 0);
    }

    public void setRotationZ(int xRotZ) {
        this.xRotZ = xRotZ;
        setAnimation(ROT_Z, xRotZ != 0);
    }

    public void setAnimType(AnimType animType) {
        mAnimType = animType;
    }

    public void setAnimData(Object animData) {
        mAnimData = animData;
    }

    public void setVisible(boolean isVisible) {
        mIsVisible = isVisible;
    }

    /** XY Zoom level. Default is 1<<16 (no zoom). */
    public void setZoomXY(int xZoomXY) {
        this.xZoomXY = xZoomXY;
        setAnimation(ZOOM, xZoomXY != 1<<16);
    }

    public void setOffset(int xOffsetX, int xOffsetY) {
        this.xOffsetX = xOffsetX;
        this.xOffsetY = xOffsetY;
        setAnimation(TRANSLATE, xOffsetX != 0 || xOffsetY != 0);
    }
}
